package controller;

import pojo.Cate;
import net.sf.json.JSONArray;
import service.CateManagement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * @author: zen
 * @date: 2020/4/15
 * @Title: CateListAjax
 * @Description: 美食列表
 */

@WebServlet("/cateList")
public class CateListAjax extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        // 获取数据对象
        ArrayList<Cate> list = CateManagement.getCates();
        // 转换JSON对象
        JSONArray jsonObject = JSONArray.fromObject(list);
        // 发回前端渲染
        PrintWriter out = resp.getWriter();
        out.write(jsonObject.toString());
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
